<?php
/**
* @version 1.0.0
* @package RSSearch! 1.0.0
* @copyright (C) 2011 www.rsjoomla.com
* @license GPL, http://www.gnu.org/copyleft/gpl.html
*/
defined('_JEXEC') or die('Restricted access');
class JElementK2searchfields extends JElement{
	var $_name = 'K2searchfields';
	
	function fetchElement($name, $value, &$node, $control_name)
	{
		if(file_exists(JPATH_SITE.DS.'components'.DS.'com_k2'.DS.'k2.php')){
			$search_fields = array(
				'k2_items' 	=> 'Items',
				'k2_categories'	=> 'Categories'
			);

			$result = '<script type="text/javascript">
				function listk2_items(checked){}
				function listk2_categories(checked){
					if(checked == true){
						document.getElementById("listk2_categories").style.visibility="visible";
						document.getElementById("desc_k2_categories").style.visibility="hidden";
					}else {
						document.getElementById("listk2_categories").style.visibility="hidden";
						document.getElementById("desc_k2_categories").style.visibility="visible";
					}
				}
			</script>';

			foreach( $search_fields as $component => $component_name){
				if(is_array( $value) ){
					if( in_array( $component, $value ) ) {
						$result .= '<label><input checked="checked" type="checkbox" name="'.$control_name.'['.$name.'][]" value="'.$component.'" onclick="list'.$component.'(this.checked);" id="'.$component.'_btn" /> '.$component_name.'</label>';
					} else {
						$result .= '<label><input type="checkbox" name="'.$control_name.'['.$name.'][]" value="'.$component.'" onclick="list'.$component.'(this.checked);" id="'.$component.'_btn" /> '.$component_name.'</label>';
					}
				} elseif ( $value ) {
					if( $value == $component) {
						$result .= '<label><input checked="checked" type="checkbox" name="'.$control_name.'['.$name.'][]" value="'.$component.'" onclick="list'.$component.'(this.checked);" id="'.$component.'_btn" /> '.$component_name.'</label>';
					} else {
						$result .= '<label><input type="checkbox" name="'.$control_name.'['.$name.'][]" value="'.$component.'" onclick="list'.$component.'(this.checked);" id="'.$component.'_btn" /> '.$component_name.'</label>';
	
					}
				} elseif ( !( $value ) ) {
					$result .= '<label><input type="checkbox" name="'.$control_name.'['.$name.'][]" value="'.$component.'" onclick="list'.$component.'(this.checked);" id="'.$component.'_btn" />'.$component_name.'</label>';
				}
			}
		} else $result = 'You need to install K2 Component in order to use RSSearch! for K2 plugin. You can download K2 component from <a href="http://getk2.org/" target="_blanc">here</a>';
		
		return $result;
	}
}
?>